using System;
using Gtk;
using System.Collections;
using System.Collections.Generic;
using bashpromptutil;

public class MainWindow: Gtk.Window
{
	protected Gtk.Button bttnClear;
	protected Gtk.Button bttnGo;
	protected Gtk.Entry manEntry;
	protected Gtk.TextView wizardBox;
	protected Gtk.TextView previewBox;
	protected Gtk.AboutDialog ad;
	//protected Gtk.MessageDialog ud;

	
	public MainWindow (): base ("")
	{
		Stetic.Gui.Build (this, typeof(MainWindow));

		previewBox.ModifyBase(StateType.Normal, new Gdk.Color(0,0,0));
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		Application.Quit ();
		a.RetVal = true;
	}

	protected virtual void OnBttnGoClicked(object sender, System.EventArgs e)
	{
		previewBox.Buffer.Clear();
		manEntry.Text = "";
		//if I type <something> in wizard box, we should check it against the
		//dictionary and, if found, print the value in the manual entry and display
		//an example of <something> in the preview box
		wiz2man(); //convert wizard buffer to escape sequences
		//TO DO:
		//man2wiz(); //convert esc. sequences to wizard text
		
		wPreviewGen();	//generates preview from wizardBox buffer		
	}

	protected virtual void OnBttnClearClicked(object sender, System.EventArgs e)
	{
		this.previewBox.Buffer.Clear();
		this.manEntry.Text = "";
		this.wizardBox.Buffer.Clear();
	}

	protected virtual void OnInsertActivated(object sender, System.EventArgs e)
	{

		Gtk.Action senderAction = (Gtk.Action)sender;
		//determine which menu item sent the event
		char[] splitters = {'(',')'};
		string[] action = senderAction.Label.Split(splitters);
		//compare action to bashEscapes
		bashEscapes be = new bashEscapes();
		if(be.escapesRev.ContainsKey(action[1]))
			this.wizardBox.Buffer.InsertAtCursor("<"+be.escapesRev[action[1]]+">");
	}

	protected virtual void OnColorActivated(object sender, System.EventArgs e)
	{
		Gtk.Action ca = (Gtk.Action)sender;
		string act = ca.Name;
		this.wizardBox.Buffer.InsertAtCursor("{"+act+"}");
	}

	private void wiz2man()
	{
		char[] delimiters = {'<','>','{','}'};
		string[] pv = wizardBox.Buffer.Text.Split(delimiters);
		bashEscapes be = new bashEscapes();
		for(int x=1;x<pv.Length;x++)
		{
			string txt = manEntry.Text;
			if(be.escapes.ContainsKey(pv[x]))
			{
				manEntry.Text = txt + be.escapes[pv[x]];
			}
			else if(be.colors.ContainsKey(pv[x]))
			{
				manEntry.Text = txt + be.colors[pv[x]];
			}
			else
			{
				manEntry.Text = txt + pv[x];
			}
				
		}
	}
	
	private void wPreviewGen()
	{
		//let's generate a preview!
		//make text tags
		string[] _rColors = {"black","red","green","orange","blue","purple","cyan","grey"};
		string[] _bColors = {"bBlack","bRed","bGreen","bBlue","bPurple","bCyan"};
		//string [] _oColors = {"yellow","white","default"};
		ArrayList rColors = new ArrayList(_rColors);
		ArrayList bColors = new ArrayList(_bColors);
		//ArrayList oColors = new ArrayList(_oColors);
		//creating arraylists makes this less complicated
		bashPreview bp = new bashPreview("{default}"+wizardBox.Buffer.Text); //assume that default is always the first color
		TextTagTable cTable = previewBox.Buffer.TagTable;
		//text tags for background and font
		TextTag bg = new TextTag("background");
		bg.Background = "black";
		cTable.Add(bg);
		TextTag font = new TextTag("font");
		font.Font = "Monospace 10";
		cTable.Add(font);
		//generate text tags for color styles
		foreach(string color in bp.colors)
		{
			TextTag tag = new TextTag(color);
			if(rColors.Contains(color))
			{
				tag.Foreground = color;
				tag.Weight = Pango.Weight.Normal;
			}
			else if(bColors.Contains(color))
			{
				char[] trim = {'b'};
				tag.Foreground = color.TrimStart(trim).ToLower();
				tag.Weight = Pango.Weight.Bold;
			}
			else
			{
				if(color == "yellow" || color == "white")
					{
						tag.Foreground = color;
						tag.Weight = Pango.Weight.Bold;
					}
				else
					{
						tag.Foreground = "white";
					}
			}
			cTable.Add(tag);				
			//previewBox.Buffer.TagTable.Add(tag);
		}//end foreach
		
		//write to buffer
		TextIter insIter = previewBox.Buffer.StartIter;
		for(int x=0;x<bp.colors.Length;x++)
		{
			previewBox.Buffer.InsertWithTagsByName(ref insIter,bp.paramL[x],bp.colors[x],"background","font");
		}
	}

	protected virtual void OnMiscActivated(object sender, System.EventArgs e)
	{
		Widget w = this.Focus;
		Gtk.Clipboard board = Clipboard.Get(Gdk.Selection.Clipboard);
		Gtk.Action senderA = (Gtk.Action)sender;
		switch(senderA.Name)
		{
			case "Exit":
				Application.Quit();
				break;
			case "Copy":
				//copy
				if(w.Name=="wizardBox")
					this.wizardBox.Buffer.CopyClipboard(board);
				else if(w.Name=="manEntry")
					this.manEntry.CopyClipboard();
				else if(w.Name=="previewBox")
					this.previewBox.Buffer.CopyClipboard(board);
				break;
			case "Cut":
				//cut
				this.wizardBox.Buffer.CutClipboard(board,true);
				break;
			case "Paste":
				//paste
				this.wizardBox.Buffer.PasteClipboard(board);
				break;
			case "Usage":
				//show usage dialog
				UsageDialog();
				break;
			case "About":
				//show about dialog
				AboutD();
				break;
		}
			
	}
	
	protected void AboutD()
	{
		ad = new AboutDialog();
		string[] authors = {"Jonathan \"lonepie\" Rogers <lonepie@bunchafunk.net>",};
		ad.Name = "BashPromptUtil v1.0 Beta";
		ad.Authors = authors;
		ad.Comments = "A wizard utility for generating a custom Bash Prompt.";
		ad.Copyright = "© 2006 Jonathan \"lonepie\" Rogers";
		ad.License = "Licensed under the GNU GPL version 2 or later.";
		ad.Website = "http://bunchafunk.net";
		ad.WebsiteLabel = "Bunchafunk.net";
		ad.Title = "About";
		ad.LogoIconName = "gtk-about";
		ad.IconName = "gtk-about";

		ad.Run();
	}
	
	protected void UsageDialog()
	{
		string message = "BashPromptUtil Usage\n--------------------------------\n\n"+
					"Simply use the \"Wizard\" Menu to "+
					"create a personalized Bash Prompt with the desired "+
					"colors and items.\n\nUse the form:\n{color}<item>character {color2}<item2>...{default}"+
					"\n(Be sure to end with {default} or else your command line's text will be colored)"+
					"\n\nPress \"Go\" to convert the \"Wizard\" "+
					"syntax to the equivalent Bash escape sequences.\n\n"+
					"If you are satisfied with the way your prompt looks, "+
					"copy the escape sequence from the \"Output\" box and "+
					"type in your terminal:\n\t PS1=\"<output escape sequence>\"\n"+
					"Paste the text from \"Output\" into the appropriate place in "+
					"the above command (Include the quotes).\n\nIf you have any questions or comments, "+
					"see the \"About\" menu for contact information.\n\n"+
					"Happy Prompt Customizing!";
		UsageDialog ud = new UsageDialog(message);
		ud.Run();

//		ud = new MessageDialog(this,Gtk.DialogFlags.Modal,
//					Gtk.MessageType.Info,Gtk.ButtonsType.Close,message2);
//		ud.Run();	                         
		
	}
	
}//end class